package com.net.xpay.core.manager.impl;

import com.google.common.base.Preconditions;
import com.net.xpay.core.constant.TransBizTypeConstant;
import com.net.common.annotation.WriteTransactional;
import com.net.common.dao.BasicMapper;
import com.net.common.dao.Page;
import com.net.common.manager.AbstractVersionedManager;
import com.net.noah.core.service.SerialNumberService;
import com.net.xpay.core.dao.UserWithdrawRecordMapper;
import com.net.xpay.core.domain.UserWithdrawRecord;
import com.net.xpay.core.manager.UserWithdrawRecordManager;
import com.net.xpay.core.model.UserWithdrawRecordSearchModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;

import java.time.LocalDateTime;
import java.util.List;


@Component
public class UserWithdrawRecordManagerImpl extends AbstractVersionedManager<UserWithdrawRecord> implements UserWithdrawRecordManager{

    @Autowired
    private UserWithdrawRecordMapper mapper;


    @Override
    protected BasicMapper<Long, UserWithdrawRecord> getMapper() {
        return mapper;
    }

    @Autowired
    private SerialNumberService serialNumberService;

    @Override
    @WriteTransactional(propagation = Propagation.SUPPORTS)
    public int save(UserWithdrawRecord obj) {
        Preconditions.checkArgument(obj.getId() == null);
        obj.setId(serialNumberService.getLongSerialNumber(getTableFullName()));
        obj.setBizNo(TransBizTypeConstant.UW + obj.getId());
        return super.save(obj);
    }

    @Override
    public List<UserWithdrawRecord> search(UserWithdrawRecordSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        Page page = new Page(searchModel);
        page.addOrder("id", "desc");
        return nonEmptyList(mapper.search(searchModel, page));
    }

    @Override
    public long searchCount(UserWithdrawRecordSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        return mapper.searchCount(searchModel);
    }

    @Override
    public long statTransferMoney(LocalDateTime startTime, LocalDateTime endTime) {
        Preconditions.checkArgument(startTime != null && endTime != null);

        return mapper.statTransferMoney(startTime, endTime);
    }
}
